<?php
    /**
     * This is used to create a new page.
     *
     * OUTPUT: Redirect to /edit on the new page.
     * SIDE EFFECT: Store _POST in database after authenticating user permissions
     */

    require_once("../../../includes.php");
    require_once("../model/page.php");
    require_once("../model/page_template.php");
    require_once("../model/user.php");
    require_once("../redirect.php");

    $url = url;

    if ( !isloggedin() )
        trigger_error('You must be logged in to create a page.', E_USER_ERROR);

    // Load Data from POST
    $user_ident = intval(required_param( 'page_user_ident' ));
    $user = folio_user::SelectWhereIdent( $user_ident );
    if ( !$user )
        trigger_error('Invalid userid '. $user_ident . ' passed to page_insert.php', E_USER_ERROR);

    $parentpage_ident = intval( optional_param( 'page_parentpage_ident',-1 ));

    // Test permissions
    if ( !folio_page::InsertPermission( $user ) )
        trigger_error("You do not have permission to create a page for user {$user->username}.", E_USER_ERROR);

    $page = null;

    // Look to see if we're supposed to create the page from a template.
    if( optional_param( 'folio_SelectTemplate', -1) != -1) {
        // Create new pages based off of a template.
        $template_name = required_param('folio_SelectTemplate');

        $pages = folio_page_template::TemplateFactory( $user,
                                                     $template_name,
                                                     '' );
        $page = folio_page::SelectHomepage($user);

    } elseif ( required_param('folio_NewPageFromTemplate_page') !== '_blank' ) {
        // Create a single page from an existing template page.

        $template_name = required_param('folio_NewPageFromTemplate_page');
        $page_name = substr( $template_name, strpos( $template_name, '::')+2);
        $template_name = substr($template_name, 0,
                                strlen( $template_name) - strlen( $page_name) -2 );

        $pages = folio_page_template::TemplateFactory( $user,
                                                     $template_name,
                                                     $page_name );
        $page = $pages[$page_name];

    } elseif ( required_param('folio_NewPageFromTemplate_page') == '_blank' ) {
        // Create a new blank page.
        $title = required_param( 'page_title' );
        $format_ident = intval( required_param( 'page_format_ident' ));

        // Validate the new title
        if( strpos( $title, '\\') !== false OR strpos( $title, '/') !== false )
            die(__gettext("Error: Titles can not contain \\ or / characters"));
        if( trim($title) == '')
            trigger_error('Empty titles are not allowed.', E_USER_ERROR);

        // Make sure that a page with that name doesn't already exist.
        $page = folio_page::SelectWhereTitleAndUsername( $title, $user->username );
        if ( !!$page ) {
            die("Sorry, but a page with the name of '$title' has already" .
                    " been created.  Please hit the 'back' button and try another" .
                    " name.");
        }

        $parentpage = folio_page::SelectHomepage( $user);

        // Create page.
        $page = folio_page::NewPageFactory($user, $title, '', $format_ident, $parentpage->page_ident );
        if ( !$page )
            trigger_error('Error: Unable to create page record.', E_USER_ERROR);

    }


    // NOte: unlike most ajax pages, insert doesn't pass a redirection
    //    This is because we don't know the page name in advance, or know
    //    what its ID will be.  insert will automatically redirect to /edit
    //    on the new page.
    $redirect = new folio_redirect;
    $redirect->redirect_url = "{$url}{$user->username}/page/" .
        folio_page::title_encode($page->title) . "/edit";
    $redirect->PerformRedirectActions();

?>
